import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store/index";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta:{isTabber:true,active:0}
  },
  {
    path: '/cloud',
    name: 'Cloud',
    component: () => import('../views/Cloud.vue'),
    meta:{isTabber:true,active:1}
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/User.vue'),
    meta:{isTabber:true,active:2,index:10}
  },
  
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta:{isTabber:false}
  },
  {
    path: '/playdetail',
    name: 'Playdetail',
    component: () => import('../views/playDetail.vue'),
    meta:{isTabber:false,index:8}
  },
  {
    path: '/songdetail',
    name: 'SongDetail',
    component: () => import('../views/songDetail.vue'),
    meta:{isTabber:false,index:5}
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Search.vue'),
    meta:{isTabber:false}
  },
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 判断是否去user
  if (to.path === '/user') {
    let token = window.localStorage.getItem('token')
    if (token) {
      return next()
    } else {
      Vue.prototype.$toast("请先登录")
      setTimeout(() => {
        next("/login");   // 跳转到user路由
      }, 1000)
    }

  }
  next()
})



export default router
